package com.jeesite.modules;

/**
 * Created by chensk on 2018/4/15.
 */

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import java.io.IOException;

/**支持水印*/
public class Watermark extends PdfPageEventHelper {


    Font FONT1 = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD, new GrayColor(0f));
    private String waterCont;//水印内容
    public Watermark() {

    }

    public Font getFont() throws IOException, DocumentException {


        BaseFont  bf = BaseFont.createFont("/fangsong.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
        Font font = new Font(bf, 15, Font.BOLD, new GrayColor(0.6f));

        return font;

    }
    public Watermark(String waterCont) {
        this.waterCont = waterCont;
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {

                try {
                    ColumnText.showTextAligned(writer.getDirectContentUnder(),
                            Element.ALIGN_CENTER,
                            new Phrase(this.waterCont == null ? "HELLO WORLD" : this.waterCont, getFont()),
                            (50.5f + i * 480),
                            (150.0f + j * 550),
                            writer.getPageNumber() % 2 == 1 ? 90 : 90);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (DocumentException e) {
                    e.printStackTrace();
                }
            }
        }
        if(writer.getPageNumber()>2) {
            ColumnText.showTextAligned(writer.getDirectContentUnder(),
                    Element.ALIGN_BOTTOM,
                    new Phrase("-" + (writer.getPageNumber()-2) + "-", FONT1),
                    275f, 20f, 0
            );
        }

    }
}